package com.project.service.impl;

import com.project.bean.CutPageBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDate;

public class UserServiceImpl extends BaseService implements IUserService {
    @Override
    public void add(UserBean user) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.add(user);
        session.commit();
        session.close();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public UserBean landFind(String name, String pwd) {
                SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.landFind(name,pwd);
        session.commit();
        session.close();
        return user;
    }

    @Override
    public UserBean findById(Integer id) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        UserBean user = mapper.findById(id);
        session.commit();
        session.close();
        return user;
    }

    @Override
    public void update(Integer id, String pwd, String grade) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
         mapper.update(id,pwd,grade);
        session.commit();
        session.close();
    }

    @Override
    public CutPageBean<UserBean> findByItem(Integer pageNo, String name, LocalDate startDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IUserMapper mapper = session.getMapper(IUserMapper.class);
        CutPageBean<UserBean> cut = new CutPageBean<>();
        cut.setList(mapper.cutList(name,startDate,endDate,(pageNo-1)*CutPageBean.PAGESIZE,CutPageBean.PAGESIZE));
        cut.setCount(mapper.cutCount(name,startDate,endDate));
        session.commit();
        session.close();
        return cut;
    }
}
